# -*- coding:utf-8 -*-
# 导包
import pymysql


# 设计数据工具类
class DBUtil():
    conn = None

    # 设计工具里的方法
    # 创连接
    @classmethod
    def __creat_conn(cls):
        if cls.conn is None:
            cls.conn = pymysql.connect(host="211.103.136.244", user="student",
                                       port=7061, password="iHRM_student_2022",
                                       database="ihrm", charset="utf8")
        return cls.conn

    # 关闭连接
    @classmethod
    def __close_conn(cls):
        if cls.conn is not None:
            cls.conn.close()
            cls.conn = None

    # 设计一个查询语句的通用方法
    @classmethod
    def select_one(cls, sql):
        curs = None
        one = None
        try:
            cls.conn = cls.__creat_conn()
            curs = cls.conn.cursor()
            curs.execute(sql)
            one = curs.fetchone()
        except Exception as err:
            print(f"错误信息是{err}")
        finally:
            curs.close()
            cls.__close_conn()
            return one

    # 设计一个增删改的通用方法
    @classmethod
    def uid_db(cls, sql):
        curs = None
        raw = None
        try:
            cls.conn = cls.__creat_conn()
            curs = cls.conn.cursor()
            curs.execute(sql)
            cls.conn.commit()  # 回滚事务
            raw = cls.conn.affected_rows()
        except Exception as err:
            print(f"错误信息是{err}")
            cls.conn.rollback()
        finally:
            curs.close()
            cls.__close_conn()
            return f"影响行数是{raw}"


if __name__ == '__main__':
    one_select = DBUtil.select_one("select * from ihrm where mobile = '13800000002'")
    print(one_select)
